package com.example.mygame;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int ROW = 3;//行 对应x
    private static final int COL = 3;//列 对应y
    private Button[][] cps_btns = null; //按钮二维数组
    private ChessPiece[][] cps = null; //棋子二维数组
    private Drawable[] drawables =null; //棋子背景资源数组
    private BtnManager btnManager = null; //棋盘按键助手
    private int selectCount = 0;//记录按钮选择数
    private Button operate_btn_A = null;//记录操作棋子A
    private Button operate_btn_B = null;//记录操作棋子B
    private Button restart_btn = null;//重开按钮
    private Button rule_btn = null;//规则按钮
    private Button return_btn = null;//返回按钮


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*关任务栏*/
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        //setContentView(R.layout.activity_main);
        //setContentView(new PlayGround(this));
        setContentView(R.layout.activity_main_2);

        /*-----------------------------------------------------------*/
        /*第一步*/
        /*初始化数组*/
        Log.i("Info", "进行初始化");
        cps_btns = new Button[ROW][COL];
        cps = new ChessPiece[ROW][COL];

        /*实例化一个BtnManager*/
        btnManager = new BtnManager(cps_btns,cps);
        /*-----------------------------------------------------------*/
        /*第二步*/
        //添加按钮监听器
        /*
          按钮监听
          已选中则 取消选择，从正在操作按钮记录移除，减少一个选择计数
          未选中则 如果记录没满的话设置为选中，添加到正在操作按钮记录，增加一个选择计数
          如果选择计数到达2则运行回合，如果返回为true代表当前选择运行成功，清空正在操作按钮记录，清空选择计数，检查是否游戏结束
         */
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                if(v.isSelected()) {
                    v.setSelected(false);
                    Set_operate_btn((Button)v);
                    selectCount--;
                    selectCount = Math.max(selectCount,0);
                }
                else {
                    if(selectCount < 2){
                        v.setSelected(true);
                        Set_operate_btn((Button)v);
                        selectCount++;
                    }
                }

                if(selectCount == 2){
                    //目标：把当前回合记录在btnmanager里面，然后传入两个棋子，btnmanager自动比对获得当前操作员那方为主动方
                    if(btnManager.RunRound(operate_btn_A,operate_btn_B)){
                        Clear_operate_btn();
                        selectCount = 0;
                        CheckIfEnd();
                    }

                }
            }
        };


        /*初始化数组元素并添加监听器*/
        Log.i("Info", "开始获取按钮");
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                //String btnID = "chess"+String.valueOf(j)+String.valueOf(i);
                String btnID = "chess" + i + "_" + j;
                int resID = getResources().getIdentifier(btnID, "id", "com.example.mygame");
                if(resID == 0) {
                    Log.i("Error", "onCreate: 找不到对应的棋子按钮");
                    return;
                    //计划：加入检测到错误退出应用
                }

                cps_btns[i][j] = (Button)findViewById(resID);
                cps_btns[i][j].setTag(i + "_" + j);
                cps_btns[i][j].setOnClickListener(listener);

                cps[i][j] = new ChessPiece(i,j);
            }
        }
        Log.i("Info", "获取按钮完成");


        //添加重置游戏按钮监听
        restart_btn = (Button)findViewById(R.id.btn_restart);
        restart_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                btnManager.ResetChessboard();
            }
        });


        return_btn = (Button)findViewById(R.id.btn_return);
        rule_btn = (Button)findViewById(R.id.btn_rule);
        //添加返回按钮监听
        return_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                return_btn.setVisibility(View.INVISIBLE);
                return_btn.setClickable(false);
                rule_btn.setVisibility(View.VISIBLE);
            }
        });
        return_btn.setVisibility(View.INVISIBLE);
        return_btn.setClickable(false);

        //添加规则按钮监听
        rule_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(MainActivity.this,MenuActivity.class));
                return_btn.setVisibility(View.VISIBLE);
                return_btn.setClickable(true);
                return_btn.bringToFront();
                rule_btn.setVisibility(View.INVISIBLE);
            }
        });

        /*-----------------------------------------------------------*/
        /*第三步*/
        /*获取并设置操作指示器*/
        ImageView pointer_red = findViewById(R.id.pointer_red);
        ImageView pointer_blue = findViewById(R.id.pointer_blue);
        btnManager.SetPointer(pointer_red,pointer_blue);

        /*设置操作员*/
        btnManager.SetOperator(ChessPiece.CAMP_RED);

        /*获取并设置按钮背景*/
        drawables = new Drawable[3];
        drawables[0] = getResources().getDrawable(R.drawable.btn_red,null);
        drawables[1] = getResources().getDrawable(R.drawable.btn_blue,null);
        drawables[2] = getResources().getDrawable(R.drawable.btn_white,null);
        btnManager.SetDrawable(drawables);
        /*-----------------------------------------------------------*/
        /*第四步*/
        /*初始化棋盘*/
        int[][] camps = {
                {1, 0, 0},
                {0, 0, 0},
                {0 ,0, 2}
        };

        int[][] poweroriginals = {
                {5, 1 ,4},
                {2, 3, 2},
                {4, 1, 5}
        };
        btnManager.InitChessboard(camps,poweroriginals);

    }


    /*-----------------------------------------------------------*/
    /**
     * 设置当前操作的按钮记录
     * 总共要记录两个操作的按钮operate_btn_A和operate_btn_B
     * 如果重复就代表点了第二次因此改为空
     * @param new_cp
     */
    private void Set_operate_btn(Button new_cp){
        if(operate_btn_A == new_cp){
            operate_btn_A = null;
        }
        else if(operate_btn_B == new_cp){
            operate_btn_B = null;
        }
        else if(operate_btn_A == null){
            operate_btn_A = new_cp;
        }
        else if(operate_btn_B == null){
            operate_btn_B = new_cp;
        }
    }

    /**
     * 清除当前操作按钮的记录
     */
    private void Clear_operate_btn(){
        operate_btn_A = null;
        operate_btn_B = null;
    }

    /**
     * 检查是否游戏结束
     * @return 是否已经有一方获胜使得游戏结束
     */
    private boolean CheckIfEnd(){
        int Winner = btnManager.GetWinner();
        if(Winner == ChessPiece.CAMP_RED){
            //红方获胜
            Toast.makeText(this,"恭喜红方取得胜利",Toast.LENGTH_SHORT).show();
            return true;
        }
        else if(Winner == ChessPiece.CAMP_BLUE){
            //蓝方获胜
            Toast.makeText(this,"恭喜蓝方取得胜利",Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
}